package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduKnowledgePointDTO;
import com.zenith.edu.po.vo.EduKnowledgePointVO;
import com.zenith.edu.service.EduKnowledgePointService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/knowledge-point")
@Tag(name = "知识点管理", description = "知识点管理接口")
public class EduKnowledgePointController {
    
    @Autowired
    private EduKnowledgePointService eduKnowledgePointService;
    
    @SecurePostMapping(value = "/createKnowledgePoint", roles = "ROLE_ADMIN", permission = "knowledge-point:create")
    @Operation(summary = "创建知识点")
    public Result<EduKnowledgePointVO> createKnowledgePoint(@RequestBody EduKnowledgePointDTO dto) {
        EduKnowledgePointVO knowledgePointVO = eduKnowledgePointService.createKnowledgePoint(dto);
        return Result.success(knowledgePointVO);
    }
    
    @SecurePostMapping(value = "/getKnowledgePointById/{id}", roles = "ROLE_ADMIN", permission = "knowledge-point:read")
    @Operation(summary = "根据ID获取知识点详情")
    public Result<EduKnowledgePointVO> getKnowledgePointById(@PathVariable Long id) {
        EduKnowledgePointVO knowledgePointVO = eduKnowledgePointService.getKnowledgePointById(id);
        if (knowledgePointVO == null) {
            return Result.fail("知识点不存在");
        }
        return Result.success(knowledgePointVO);
    }
    
    @SecurePostMapping(value = "/updateKnowledgePoint", roles = "ROLE_ADMIN", permission = "knowledge-point:update")
    @Operation(summary = "更新知识点信息")
    public Result<EduKnowledgePointVO> updateKnowledgePoint(@RequestBody EduKnowledgePointDTO dto) {
        EduKnowledgePointVO knowledgePointVO = eduKnowledgePointService.updateKnowledgePoint(dto);
        return Result.success(knowledgePointVO);
    }
    
    @SecurePostMapping(value = "/deleteKnowledgePoint/{id}", roles = "ROLE_ADMIN", permission = "knowledge-point:delete")
    @Operation(summary = "删除指定知识点")
    public Result<Void> deleteKnowledgePoint(@PathVariable Long id) {
        boolean result = eduKnowledgePointService.deleteKnowledgePoint(id);
        if (!result) {
            return Result.fail("知识点删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getKnowledgePointList", roles = "ROLE_ADMIN", permission = "knowledge-point:read")
    @Operation(summary = "获取知识点列表")
    public Result<List<EduKnowledgePointVO>> getKnowledgePointList() {
        List<EduKnowledgePointVO> knowledgePointList = eduKnowledgePointService.getKnowledgePointList();
        return Result.success(knowledgePointList);
    }
}